package dxcTopic;

public class sync_Test {


    private int num;
    private static final Object LOCK = new Object();

    private void printABC(int targetNum) {
        synchronized (LOCK) {
            while (num % 3 != targetNum) {    //想想这里为什么不能用if代替while，想不起来可以看公众号上一篇文章
                try {
                    LOCK.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            num++;
            System.out.print(Thread.currentThread().getName());
            LOCK.notifyAll();
        }


    }

    public static void main(String[] args) {
        sync_Test wait_notify_acb = new sync_Test();
        System.out.println(0%3);
        System.out.println(1%3);
        System.out.println(2%3);

        new Thread(() -> {
            wait_notify_acb.printABC(0);
        }, "A").start();
        new Thread(() -> {
            wait_notify_acb.printABC(1);
        }, "B").start();
        new Thread(() -> {
            wait_notify_acb.printABC(2);
        }, "C").start();
    }
}


